home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / quopri.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  7KB  |  270 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Conversions to/from quoted-printable transport encoding as per RFC 1521.'''
  5. __all__ = [
  6.     'encode',
  7.     'decode',
  8.     'encodestring',
  9.     'decodestring']
  10. ESCAPE = '='
  11. MAXLINESIZE = 76
  12. HEX = '0123456789ABCDEF'
  13. EMPTYSTRING = ''
  14.  
  15. try:
  16.     from binascii import a2b_qp, b2a_qp
  17. except ImportError:
  18.     a2b_qp = None
  19.     b2a_qp = None
  20.  
  21.  
  22. def needsquoting(c, quotetabs, header):
  23.     """Decide whether a particular character needs to be quoted.
  24.  
  25.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  26.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  27.     RFC 1521.
  28.     """
  29.     if c in ' \t':
  30.         return quotetabs
  31.     return c in ' \t' if c == '_' else not c == '_' if c <= c else c <= '~'
  32.  
  33.  
  34. def quote(c):
  35.     '''Quote a single character.'''
  36.     i = ord(c)
  37.     return ESCAPE + HEX[i // 16] + HEX[i % 16]
  38.  
  39.  
  40. def encode(input, output, quotetabs, header = 0):
  41.     """Read 'input', apply quoted-printable encoding, and write to 'output'.
  42.  
  43.     'input' and 'output' are files with readline() and write() methods.
  44.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  45.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  46.     RFC 1521.
  47.     The 'header' flag indicates whether we are encoding spaces as _ as per
  48.     RFC 1522.
  49.     """
  50.     if b2a_qp is not None:
  51.         data = input.read()
  52.         odata = b2a_qp(data, quotetabs = quotetabs, header = header)
  53.         output.write(odata)
  54.         return None
  55.     
  56.     def write(s, output = output, lineEnd = '\n'):
  57.         if s and s[-1:] in ' \t':
  58.             output.write(s[:-1] + quote(s[-1]) + lineEnd)
  59.         elif s == '.':
  60.             output.write(quote(s) + lineEnd)
  61.         else:
  62.             output.write(s + lineEnd)
  63.  
  64.     prevline = None
  65.     while None:
  66.         line = input.readline()
  67.         if not line:
  68.             break
  69.         
  70.         outline = []
  71.         stripped = ''
  72.         if line[-1:] == '\n':
  73.             line = line[:-1]
  74.             stripped = '\n'
  75.         
  76.         for c in line:
  77.             if needsquoting(c, quotetabs, header):
  78.                 c = quote(c)
  79.             
  80.             if header and c == ' ':
  81.                 outline.append('_')
  82.                 continue
  83.             outline.append(c)
  84.         
  85.         if prevline is not None:
  86.             write(prevline)
  87.         
  88.         thisline = EMPTYSTRING.join(outline)
  89.         while len(thisline) > MAXLINESIZE:
  90.             write(thisline[:MAXLINESIZE - 1], lineEnd = '=\n')
  91.             thisline = thisline[MAXLINESIZE - 1:]
  92.         prevline = thisline
  93.         continue
  94.         if prevline is not None:
  95.             write(prevline, lineEnd = stripped)
  96.         
  97.  
  98.  
  99. def encodestring(s, quotetabs = 0, header = 0):
  100.     if b2a_qp is not None:
  101.         return b2a_qp(s, quotetabs = quotetabs, header = header)
  102.     StringIO = StringIO
  103.     import cStringIO
  104.     infp = StringIO(s)
  105.     outfp = StringIO()
  106.     encode(infp, outfp, quotetabs, header)
  107.     return outfp.getvalue()
  108.  
  109.  
  110. def decode(input, output, header = 0):
  111.     """Read 'input', apply quoted-printable decoding, and write to 'output'.
  112.     'input' and 'output' are files with readline() and write() methods.
  113.     If 'header' is true, decode underscore as space (per RFC 1522)."""
  114.     if a2b_qp is not None:
  115.         data = input.read()
  116.         odata = a2b_qp(data, header = header)
  117.         output.write(odata)
  118.         return None
  119.     new = ''
  120.     while None:
  121.         line = input.readline()
  122.         if not line:
  123.             break
  124.         
  125.         i = 0
  126.         n = len(line)
  127.         if n > 0 and line[n - 1] == '\n':
  128.             partial = 0
  129.             n = n - 1
  130.             while n > 0 and line[n - 1] in ' \t\r':
  131.                 n = n - 1
  132.         else:
  133.             partial = 1
  134.         while i < n:
  135.             c = line[i]
  136.             if c == '_' and header:
  137.                 new = new + ' '
  138.                 i = i + 1
  139.                 continue
  140.             if c != ESCAPE:
  141.                 new = new + c
  142.                 i = i + 1
  143.                 continue
  144.             if i + 1 == n and not partial:
  145.                 partial = 1
  146.                 break
  147.                 continue
  148.             if i + 1 < n and line[i + 1] == ESCAPE:
  149.                 new = new + ESCAPE
  150.                 i = i + 2
  151.                 continue
  152.             if i + 2 < n and ishex(line[i + 1]) and ishex(line[i + 2]):
  153.                 new = new + chr(unhex(line[i + 1:i + 3]))
  154.                 i = i + 3
  155.                 continue
  156.             new = new + c
  157.             i = i + 1
  158.         if not partial:
  159.             output.write(new + '\n')
  160.             new = ''
  161.             continue
  162.         continue
  163.         if new:
  164.             output.write(new)
  165.         
  166.  
  167.  
  168. def decodestring(s, header = 0):
  169.     if a2b_qp is not None:
  170.         return a2b_qp(s, header = header)
  171.     StringIO = StringIO
  172.     import cStringIO
  173.     infp = StringIO(s)
  174.     outfp = StringIO()
  175.     decode(infp, outfp, header = header)
  176.     return outfp.getvalue()
  177.  
  178.  
  179. def ishex(c):
  180.     """Return true if the character 'c' is a hexadecimal digit."""
  181.     return None if c <= c else 'A' if c <= c else c
  182.  
  183.  
  184. def unhex(s):
  185.     '''Get the integer value of a hexadecimal number.'''
  186.     bits = 0
  187.     for c in s:
  188.         if c <= c:
  189.             pass
  190.         elif c <= '9':
  191.             i = ord('0')
  192.         elif c <= c:
  193.             pass
  194.         elif c <= 'f':
  195.             i = ord('a') - 10
  196.         elif c <= c:
  197.             pass
  198.         elif c <= 'F':
  199.             i = ord('A') - 10
  200.         else:
  201.             break
  202.         bits = bits * 16 + (ord(c) - i)
  203.     
  204.     return bits
  205.  
  206.  
  207. def main():
  208.     import sys as sys
  209.     import getopt as getopt
  210.     
  211.     try:
  212.         (opts, args) = getopt.getopt(sys.argv[1:], 'td')
  213.     except getopt.error:
  214.         msg = None
  215.         sys.stdout = sys.stderr
  216.         print msg
  217.         print 'usage: quopri [-t | -d] [file] ...'
  218.         print '-t: quote tabs'
  219.         print '-d: decode; default encode'
  220.         sys.exit(2)
  221.  
  222.     deco = 0
  223.     tabs = 0
  224.     for o, a in opts:
  225.         if o == '-t':
  226.             tabs = 1
  227.         
  228.         if o == '-d':
  229.             deco = 1
  230.             continue
  231.     
  232.     if tabs and deco:
  233.         sys.stdout = sys.stderr
  234.         print '-t and -d are mutually exclusive'
  235.         sys.exit(2)
  236.     
  237.     if not args:
  238.         args = [
  239.             '-']
  240.     
  241.     sts = 0
  242.     for file in args:
  243.         if file == '-':
  244.             fp = sys.stdin
  245.         else:
  246.             
  247.             try:
  248.                 fp = open(file)
  249.             except IOError:
  250.                 msg = None
  251.                 sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
  252.                 sts = 1
  253.                 continue
  254.  
  255.         if deco:
  256.             decode(fp, sys.stdout)
  257.         else:
  258.             encode(fp, sys.stdout, tabs)
  259.         if fp is not sys.stdin:
  260.             fp.close()
  261.             continue
  262.     
  263.     if sts:
  264.         sys.exit(sts)
  265.     
  266.  
  267. if __name__ == '__main__':
  268.     main()
  269.  
  270.